package edu.gyc.hiseckill.controller;


import edu.gyc.hiseckill.code.CodeGenerateThread;
import edu.gyc.hiseckill.code.OrderThread;
import edu.gyc.hiseckill.dao.RandomCodeDao;
import edu.gyc.hiseckill.service.UserOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-01-29
 */
@RestController
@Slf4j
public class RandomCodeController {
    @Autowired
    private RandomCodeDao randomCodeMapper;

    @Autowired
    private UserOrderService userOrderService;

    //测试在高并发下多线程生成订单编号-传统的随机数生成方法,UUID,
    @RequestMapping(value = "/hiseq",method = RequestMethod.GET)
    public Object codeThread(){

        try {
            ExecutorService executorService= Executors.newFixedThreadPool(10);
            for (int i=0;i<1000;i++){
                executorService.execute(new CodeGenerateThread(randomCodeMapper));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    //测试在高并发下是否会超卖，结果订单数正常，但商品销售数量不正常
    @RequestMapping(value = "/hiorder",method = RequestMethod.GET)
    public Object hiorder(){

        try {
            ExecutorService executorService= Executors.newFixedThreadPool(10);
            for (int i=0;i<1000;i++){
                executorService.execute(new OrderThread(userOrderService));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
}

